﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

namespace Bronze.ReportBuilder.BusinessComponent
{
    [Serializable]
    public class BusinessAttribute : Attribute
    {
        public BusinessAttribute(string identify, string name)
            : this(identify,name, true)
        {
        }

        public BusinessAttribute(string identify, string name, bool canBeTopLevel)
        {
            this.Identify = new Guid(identify);
            this.name = name;
            this.canBeTopLevel = canBeTopLevel;
        }

        public Guid Identify
        {
            get;
            set;
        }

        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Type OwnerType
        {
            get;
            set;
        }

        bool canBeTopLevel = true;

        /// <summary>
        /// 是否可以作为第一层选择框
        /// </summary>
        public bool CanBeTopLevel
        {
            get { return canBeTopLevel; }
            set { canBeTopLevel = value; }
        }

        public static BusinessAttribute GetBusiness(Type t)
        {
            var attrs = t.GetCustomAttributes(typeof(BusinessAttribute), true);
            if (attrs.Length > 0)
            {
                var bn= attrs[0] as BusinessAttribute;
                bn.OwnerType = t;
                return bn;
            }
            return null;
        }

        public static bool ExsitRelation(Type t, Type relationType)
        {
            List<RelationAttribute> attrs = new List<RelationAttribute>();
            var props = t.GetProperties();
            foreach (var prop in props)
            {
                var attr = RelationAttribute.GetAttr(prop);
                if (attr != null && attr.Type == relationType)
                {
                    return true;
                }
            }
            return false;
        }

        public static PropertyInfo GetRelationProperty(Type t, Type relationType)
        {
            List<RelationAttribute> attrs = new List<RelationAttribute>();
            var props = t.GetProperties();
            foreach (var prop in props)
            {
                var attr = RelationAttribute.GetAttr(prop);
                if (attr != null && attr.Type == relationType)
                {
                    return prop;
                }
            }
            return null;
        }


        static List<BusinessAttribute> _comboBusiness;
        public static List<BusinessAttribute> ComboBusiness
        {
            get
            {
                if (_comboBusiness == null)
                {
                    GetAllBusiness();
                }
                return _comboBusiness;
            }
        }

        static List<BusinessAttribute> _treeComboBusiness;
        public static List<BusinessAttribute> TreeComboBusiness
        {
            get
            {
                if (_treeComboBusiness == null)
                {
                    GetAllBusiness();
                }
                return _treeComboBusiness;
            }
        }
       

        public static void GetAllBusiness()
        {
            _comboBusiness = new List<BusinessAttribute>();
            _treeComboBusiness = new List<BusinessAttribute>();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                var types = assembly.GetTypes();
                foreach (var type in types)
                {
                    if (type != typeof(LabelFormControl) && typeof(LabelFormControl).IsAssignableFrom(type))
                    {
                        var business = BusinessAttribute.GetBusiness(type);
                        if (business != null)
                        {
                            if (typeof(LabelComboTree).IsAssignableFrom(type))
                            {
                                _treeComboBusiness.Add(business);
                            }
                            else
                            {
                                _comboBusiness.Add(business);
                            }
                        }
                    }
                }
            }
        }
    }

    [Serializable]
    public class RelationAttribute : Attribute
    {
        public RelationAttribute()
        {

        }
        public RelationAttribute(Type type, bool necessary)
        {
            this.necessary = necessary;
            this.type = type;
        }

        public RelationAttribute(Type type)
            : this(type,false)
        {
        }

        Type type;

        public Type Type
        {
            get { return type; }
            set { type = value; }
        }

        bool necessary=false;

        /// <summary>
        /// 必须的
        /// </summary>
        public bool Necessary
        {
            get { return necessary; }
            set { necessary = value; }
        }

        public static RelationAttribute GetAttr(PropertyInfo prop)
        {
            var attrs = prop.GetCustomAttributes(typeof(RelationAttribute), true);
            if (attrs.Length > 0)
            {
                return attrs[0] as RelationAttribute;
            }
            return null;
        }


    }

}